﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Story.Library;
using System.Reflection;
using System.Threading;

namespace Story.Framework.WinUI
{
    public partial class PageBase : UserControl
    {
        public PageBase()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            if (this.DesignMode) return;
            RefreshClick();
            base.OnLoad(e);
        }

        /// <summary>
        /// 通过Page的类型信息字符串创建Page的实例
        /// </summary>
        /// <param name="typeInfoStr">类型信息字符串，如："Storys.Framework.WinUI.MainPage,Storys.Framework.WinUI"</param>
        /// <param name="paramsStr">参数健值对字符串,如："Title=主页;"</param>
        /// <returns></returns>
        public static PageBase Create(string typeInfoStr, string paramsStr)
        {
            string typeName = typeInfoStr.Split(',')[0].Trim();
            string assemblyName = String.Format("{0}{1}.dll", ReflectionHelper.ExecutingPath, typeInfoStr.Split(',')[1].Trim());
            Assembly assebly = Assembly.LoadFrom(assemblyName);
            PageBase p = null;
            if (!string.IsNullOrEmpty(paramsStr))
            {
                Dictionary<string, string> dic = StringHelper.GetKeyValuePairString(paramsStr);
                Type t = assebly.GetType(typeName);
                p = (PageBase)Activator.CreateInstance(t, new object[] { dic });
                if (dic.ContainsKey("Title"))
                    p.Title = dic["Title"];
            }
            else
                p = (PageBase)assebly.CreateInstance(typeName);
            return p;

        }
        public virtual void SendMessageDo(string key, string msg)
        {
        }
        public virtual void RefreshClick()
        {
            Thread th = new Thread(new ThreadStart(RefreshData));
            biSave.Enabled = false;
            biRefresh.Enabled = false;
            th.Start();
        }
        /// <summary>
        /// 刷新（载入）数据的基本实现
        /// 应该重写此方法来达到真正的数据载入和逻辑显示，并且在载入成功之后调用 base.RefreData()
        /// </summary>
        public virtual void RefreshData()
        {
            if (this.InvokeRequired)
            {
                ClientDelegate d = DoRefreData;
                this.BeginInvoke(d);
            }
            else
            {
                DoRefreData();
            }

        }

        private void DoRefreData()
        {
            biRefresh.Enabled = true;
            biSave.Enabled = true;
        }

        /// <summary>
        /// 保存事件，开启一个线程进行数据保存，此方法可以被重写。
        /// 但是一般情况应当重写UpdateData方法来达到数据提交的效果
        /// 
        /// </summary>
        public virtual void UpdateButtonClick()
        {
            Thread th = new Thread(new ThreadStart(UpdateData));
            biSave.Enabled = false;
            biRefresh.Enabled=false;
            th.Start();
        }

        /// <summary>
        /// 保存（修改）数据的基本实现
        /// 应该重写此方法来达到真正的数据载入和逻辑实现，并且在保存成功之后调用 base.UpdateData()
        /// </summary>
        public virtual void UpdateData()
        {
            if (this.InvokeRequired)
            {
                ClientDelegate d = DoUpdateData;
                this.Invoke(d);
            }
            else
            {
                DoUpdateData();
            }
        }
        private void DoUpdateData()
        {
            biSave.Enabled = true;
            biRefresh.Enabled=true;
            //保存成功
        }

        private string m_Title;

        /// <summary>
        /// 获取和设置页面标题
        /// </summary>
        public string Title
        {
            get { return m_Title; }
            set { m_Title = value; }
        }

        private void bIClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AppClient.MainForm.ClosePage(this);
        }

        private void biSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UpdateButtonClick();
        }

        private void biRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
      
    }
}
